﻿#region License
//
// Author: Pål Fossmo <pal@fossmo.net>
// Copyright (c) 2008 Pål Fossmo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

using System.IO;
using Autofac;
using Autofac.Builder;
using TreeFrog.Converter;
using TreeFrog.Document;
using TreeFrog.FileManager;
using TreeFrog.Package;
using TreeFrog.Rename;
using TreeFrog.Structure;
using TreeFrog.Textmodification;

namespace TreeFrog
{
    public class TreeFrogContainer
    {
        public static void Container(string solutionStructure, string creationFolder, string projectName)
        {
            var builder = new ContainerBuilder();
            builder.Register<DocumentService>().As<IDocument>().WithArguments(new Parameter("solutionStructure",
                                                                                            Path.GetFileNameWithoutExtension(solutionStructure)));
            builder.Register<StructureService>().As<IStructure>().ContainerScoped();
            builder.Register<FileManagerService>().As<IFileManager>().ContainerScoped();
            builder.Register<PackageService>().As<IPackage>().ContainerScoped();
            builder.Register<TextReplaceService>().As<ITextReplaceService>().ContainerScoped();
            builder.Register<RenameService>().As<IRenameService>().ContainerScoped();
            builder.Register<SolutionConverter>().As<IConverter>().ContainerScoped();
            builder.Register<TreeFrog.Rename.Rename>().As<IRename>().ContainerScoped();

            var container = builder.Build();

            var document = container.Resolve<IDocument>();
            document.GetXml();

            var structure = container.Resolve<IStructure>();
            structure.InitFolder = creationFolder;
            structure.Create();

            var filemanager = container.Resolve<IFileManager>();
            filemanager.InitFolder = creationFolder;
            filemanager.StartCopying();

            var package = container.Resolve<IPackage>();
            package.InitFolder = creationFolder;
            package.unPack();

            var textReplacer = container.Resolve<ITextReplaceService>();
            textReplacer.InitFolder = Path.Combine(Path.Combine(creationFolder, "TreeFrog"), "TreeFrog.sln");
            textReplacer.Replace();

            var rename = container.Resolve<IRename>();
            rename.WorkingDirectory = creationFolder;
            rename.DirectoryToReplace = "TreeFrog";
            rename.NewDirectory = projectName;
            rename.TextToReplace = "TreeFrog";
            rename.NewText = projectName;

            var renameService = container.Resolve<IRenameService>();
            renameService.Run();
        }
    }
}
